<?php
/**
 * Created by PhpStorm.
 * User: longjun-mi
 * Date: 2020-02-29
 * Time: 18:43
 */
namespace duoge\dingtalk;
use duoge\dingtalk\domain\At;
use duoge\dingtalk\domain\Msg;
use duoge\dingtalk\domain\Text;
use yii\base\BaseObject;

class RobotSend extends BaseObject {


    public $access_token;



    public function __construct($_access_token)
    {
        $this->access_token = $_access_token;
        parent::__construct($config);
    }



    function init()
    {

        parent::init(); // TODO: Change the autogenerated stub
    }


    private function curl_post($post_string) {
        $url = "https://oapi.dingtalk.com/robot/send?access_token=".$this->access_token;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_string));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
        // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }


    public function sss() {

        $msg = new Msg;
        $msg->msgtype="text";
        $text = new Text;
        $text->content="sfasdfsf";
        $msg->text = $text;

        $at = new At();
        $at->at_mobiles = '15967942960';
        $at->is_at_all = 'false';

        $msg->at = $at;
        print_r($msg);



        print_r($this->curl_post($msg));
    }

}